﻿using DataAccess;
using DevExpress.XtraEditors;
using QuanLyKhachSan.Validation;
using System;
using System.Linq;
using System.Windows.Forms;
using NLog;

namespace QuanLyKhachSan
{
    public partial class frmThongTinKhachHang : XtraForm
    {
        KhachHangManager controller = new KhachHangManager();
        QuocTichManager qt = new QuocTichManager();
        protected static Logger _logger = LogManager.GetCurrentClassLogger();
        public KhachHang kh=null;
        public bool themthanhcong = false, capnhatthanhcong=false;
        bool capnhat,them=false;
        public frmThongTinKhachHang()
        {
            InitializeComponent();
        }
        public frmThongTinKhachHang(bool them)
        {
            InitializeComponent();
            this.them = them;
        }
        public frmThongTinKhachHang(KhachHang kh)
        {
            InitializeComponent();
            this.capnhat = !them;
            this.kh = kh;
        }
        private void frmThongTinKhachHang_Load(object sender, EventArgs e)
        {
            this.quocTichTableAdapter.Fill(this.qLKHACHSANDataSet.QuocTich);
            FormLoad();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (them)
            {
                ThemKhachHang();
            }
            else
            {
                CapNhatKhachHang();
            }
        }

        private void ThemKhachHang()
        {
            kh = new KhachHang();
            kh.MaKH = mtxtKhachHang.Text;
            kh.HoTenKH = txtTenKhachhang.Text;
            if (cbQuocTich.SelectedIndex==-1)
            {
                kh.MaQT = "";
            }
            else
            {
                kh.MaQT = cbQuocTich.SelectedValue.ToString();
            }
            kh.GioiTinh = (rdNam.Checked) ? true : false;
            kh.NgheNghiep = txtNgheNghiep.Text;
            kh.SDT = txtSDT.Text;
            kh.NgaySinh = dtpNgaySinh.Value;
            kh.TrangThaiDL = "Đã lưu vào CSDL";

            ValidateResult vResult = controller.Validate(kh);
            if (!vResult.Message.Any())
            {
                ActionResult aResult = controller.Them(kh);
                if (!aResult.Exceptions.Any())
                {
                    MessageBox.Show("Thêm thành công khách hàng ID= " + kh.MaKH);
                    themthanhcong = true;
                    _logger.Info("Thêm thành công khách hàng ID= " + kh.MaKH);
                    this.Close();
                }
                else
                {
                    string s = "";
                    foreach (var item in aResult.Exceptions)
                    {
                        s += item.Message + "\n";
                    }
                    MessageBox.Show(s);
                    _logger.Error(s);
                }
            }
            else
            {
                string s = "";
                foreach (var item in vResult.Message)
                {
                    s += item + "\n";
                }
                MessageBox.Show(s);
                _logger.Error(s);
            }
        }

        private void CapNhatKhachHang()
        {
            kh = new KhachHang();
            kh.MaKH = mtxtKhachHang.Text;
            kh.HoTenKH = txtTenKhachhang.Text;
            kh.MaQT = cbQuocTich.SelectedValue.ToString();
            kh.GioiTinh = (rdNam.Checked) ? true : false;
            kh.NgheNghiep = txtNgheNghiep.Text;
            kh.SDT = txtSDT.Text;
            kh.NgaySinh = dtpNgaySinh.Value;
            kh.TrangThaiDL = "Đã lưu vào CSDL";

            ValidateResult vResult = controller.Validate(kh);
            if (!vResult.Message.Any())
            {
                ActionResult aResult = controller.CapNhat(kh);
                if (!aResult.Exceptions.Any())
                {
                    MessageBox.Show("Cập nhật thành công khách hàng ID= " + kh.MaKH);
                    capnhatthanhcong = true;
                    _logger.Info("Cập nhật thành công khách hàng ID= " + kh.MaKH);
                    this.Close();
                }
                else
                {
                    string s = "";
                    foreach (var item in aResult.Exceptions)
                    {
                        s += item.Message + "\n";
                    }
                    MessageBox.Show(s);
                    _logger.Error(s);
                }
            }
            else
            {
                string s = "";
                foreach (var item in vResult.Message)
                {
                    s += item + "\n";
                }
                MessageBox.Show(s);
                _logger.Error(s);
            }
        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            FormLoad();
        }

        private void FormLoad()
        {
            if (them)
            {
                mtxtKhachHang.Text = "";
                txtNgheNghiep.Text = "";
                txtSDT.Text = "";
                txtTenKhachhang.Text = "";
                dtpNgaySinh.Value = DateTime.Now;
                cbQuocTich.SelectedIndex = -1;
                rdNam.Checked = true;
            }
            else
            {
                
                mtxtKhachHang.Enabled = false;
                mtxtKhachHang.Text = kh.MaKH;
                txtNgheNghiep.Text = kh.NgheNghiep;
                txtSDT.Text = kh.SDT;
                txtTenKhachhang.Text = kh.HoTenKH;
                dtpNgaySinh.Value = kh.NgaySinh;
                cbQuocTich.SelectedValue = kh.MaQT;
              
                if (kh.GioiTinh)
                {
                    rdNam.Checked = true;
                }
                else
                {
                    rdNu.Checked = true;
                }
            }
        }

        private void btnThemQuocTich_Click(object sender, EventArgs e)
        {
            FormQuocTich frm = new FormQuocTich();
            frm.ShowDialog();
            this.quocTichTableAdapter.Fill(this.qLKHACHSANDataSet.QuocTich);
        }

    }
}
